package com.example.leon.demo.Base;

import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.ProgressBar;

import com.nostra13.universalimageloader.core.ImageLoader;

//import ctrip.android.view.destination.common.GSLoadModeSwitch;

/*
 * ImageLoaderHelper路径不要变化，它会load一个JNI, JNI访问路径固定的。
 */

public class ImageLoaderHelper {



    public static void displaySmallImage(ImageView imageView, String imageUrl) {

        ImageLoader.getInstance().displayImage(getImageUrl(imageUrl), imageView);
    }

    private static String getImageUrl(String imageUrl) {
        //return GSLoadModeSwitch.getInstance().getImageUrl(imageUrl);
        return imageUrl;
    }

    private final static int DEFAULT_BLUR_RADIUS_IN_PX = 20;
    private final static double RADIUS_PX_SCALE = 0.05;
    private static int currentRadiusInPx;

    public static Bitmap blurBitmap(Bitmap originalBitmap, int radius) {
        if (originalBitmap == null || originalBitmap.isRecycled())
            return null;

        if (radius <= 0)
            return originalBitmap;

        if (currentRadiusInPx >= (int) (DEFAULT_BLUR_RADIUS_IN_PX * RADIUS_PX_SCALE))
            currentRadiusInPx = 0;
        int real = (int) (radius * RADIUS_PX_SCALE);
        if (real == currentRadiusInPx)
            return null;
        currentRadiusInPx = real;

        Matrix scaleMatrix = new Matrix();
        float ratio = (200.0f / originalBitmap.getWidth());
        scaleMatrix.setScale(ratio, ratio);
        Bitmap bitmap = Bitmap.createBitmap(originalBitmap, 0, 0,
                originalBitmap.getWidth(), originalBitmap.getHeight(), scaleMatrix,
                true);

        int w = bitmap.getWidth();
        int h = bitmap.getHeight();

        int[] pix = new int[w * h];
        bitmap.getPixels(pix, 0, w, 0, 0, w, h);

        // The blurred image is returned back in pix
        blurImageNative(pix, w, h, currentRadiusInPx);

        bitmap.setPixels(pix, 0, w, 0, 0, w, h);

        return bitmap;
    }

    private native static void blurImageNative(int[] input, int width,
                                               int height, int radius);

    static {
        try {
            System.loadLibrary("jni_blur");
        } catch (Throwable t) {
            System.loadLibrary("jni_blur");
        }
    }
}
